package com.zm.spring.demo;

import com.zm.spring.demo.biz.UserBiz;
import com.zm.spring.demo.mapper.UserMapper;
import com.zm.spring.demo.web.UserController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.NestedTestConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class SpringDemo {
    @Test
    public void test(){
        ClassPathXmlApplicationContext cxt =
                new ClassPathXmlApplicationContext("bean.xml");
        Person person = cxt.getBean(Person.class);
        person.setName("张三");
        person.hello();

        Person person1 = (Person)cxt.getBean("Person1");
        Person person2 = (Person)cxt.getBean("Person2");

        person1.hello();
        person2.hello();
        System.out.println("################");
        Person person3 = (Person)cxt.getBean("Person3");
        System.out.println("person3 = " + person3);
        Person person31 = (Person)cxt.getBean("Person3");
        Person person32 = (Person)cxt.getBean("Person3");
        Person person33 = (Person)cxt.getBean("Person3");
        System.out.println(person3==person31);
        System.out.println(person3==person32);
        System.out.println(person3==person33);

        cxt.close();
    }


    @Test
    public void test2(){
        AnnotationConfigApplicationContext cxt =
                new AnnotationConfigApplicationContext(AnnoConfig.class);
        Person person = cxt.getBean(Person.class);
        person.setName("张三");
        person.hello();

        Person person1 = (Person)cxt.getBean("person1");
        Person person2 = (Person)cxt.getBean("person2");

        person1.hello();
        person2.hello();
        System.out.println("################");
        Person person3 = (Person)cxt.getBean("person3");
        System.out.println("person3 = " + person3);
        Person person31 = (Person)cxt.getBean("person3");
        Person person32 = (Person)cxt.getBean("person3");
        Person person33 = (Person)cxt.getBean("person3");
        System.out.println(person3==person31);
        System.out.println(person3==person32);
        System.out.println(person3==person33);

        cxt.close();
    }
    @Autowired  //自动配置注解
    @Qualifier("userControllerAuto")  //限定词
    UserController userController;

    @Autowired  //自动配置注解
    @Qualifier("userBizAuto")  //限定词
    UserBiz userBiz;

    @Autowired  //自动配置注解
    UserMapper userMapper;
    @Test
    public void test3(){
        System.out.println("userController = " + userController);
        System.out.println("userBiz = " + userBiz);
        System.out.println("userMapper = " + userMapper);
    }
}
